<template>
	<view class="content">
		<view class="menu-block block">
			<view class="title">{{info.activityName}}</view>
			<image class="active-bg" :src="cloudStorage+info.cover" mode="widthFix"></image>
			<view class="price-block">
				<view class="price-title">
					<view class="price-top">
						<template v-if="info.classification!=1">
							<view class="current-price">{{info.price||'暂无'}}</view>
							<view class="original-price">￥{{info.originalPrice||'暂无'}}
								<view class="price-line"></view>
							</view>
						</template>
						<template v-else>
							<view class="current-price">限时免费</view>
							<view class="price-line"></view>
						</template>
					</view>
					<view class="price-bottom">当前仅剩<span
							style="padding: 5rpx;font-size: 32rpx;font-weight: bold;">{{info.number||'暂无'}}</span>份
					</view>
				</view>
				<view class="price-time" v-if="info.endTime!=null">
					<view class="price-time-desc">距结束仅剩</view>
					<view class="price-time-countdown">
						<view class="time-line"><span class="text">{{hours||0}}</span></view>
						<view class="time-point">:</view>
						<view class="time-line"><span class="text">{{minutes||0}}</span></view>
						<view class="time-point">:</view>
						<view class="time-line"><span class="text">{{seconds||0}}</span></view>
					</view>
				</view>
			</view>
			<view class="tips-block">
				<view class="tips">消费须知</view>
				<!-- <view class="hint">{{info.consumeNotice||'暂无'}}</view> -->
				<view class="hint">有效期内使用，过期自动作废</view>
			</view>
			<view class="tips-block">
				<view class="tips">服务保障</view>
				<view class="hint">{{info.serviceGuarantee||'暂无'}}</view>
			</view>
			<!-- <view class="tips-block">
				<view class="tips">剩余库存</view>
				<view class="hint">{{info.number||0}}单</view>
			</view> -->
			<view class="tips-block">
				<view class="tips">有效期限</view>
				<view class="hint">{{info.validPeriod||'暂无'}}</view>
			</view>
			<view class="line"></view>
			<view class="tips-title" v-if="info.classification==1">活动规则</view>
			<view class="tips">1.本活动仅限会员抢购，每位会员每天仅限抢购1单。 </view>
			<view class="tips">2.每笔限时免费订单的有效期为:订单抢购成功后的24小时，到期自动作废。 </view>
			<view class="tips">3.本活动仅限于酒泉市肃州区商家，需到店使用。 </view>
			<view class="tips">4.本活动不与其他活动叠加使用。 </view>
		</view>
		<!-- 	<view class="small-title">团购详情</view>
		<view class="info-block">
			<view class="special-text">{{info.groupBuyingDetails||'暂无'}}</view>
		</view> -->
		<template v-if="info.groupBuyingCategoryList.length>0">
			<view class="small-title">团购详情</view>
			<view class="group-block">
				<view class="group-list" v-for="item in info.groupBuyingCategoryList" :key="item.id">
					<view class="group-title-item">
						<view class="round"></view>
						<view class="group-title">{{item.categoryName}}</view>
					</view>
					<view class="group-item" v-for="(item1,index) in item.classificationSubclasses" :key="item1.id">
						<view class="name">{{item1.name}}</view>
						<view class="count-item">
							<view class="count" v-if="item1.number">({{item1.number}}{{item1.unit}})</view>
							<view class="price1" v-if="item1.price">￥{{item1.price}}</view>
						</view>
					</view>
				</view>
			</view>
		</template>
		<template v-if="info.businessList.length>0">
			<view class="small-title">适用门店</view>
			<view class="info-block">
				<view class="shop" v-for="item in info.businessList" :key="item.id"
					@click="navigate('/page_other/activity/merchant?id='+item.id+'&longitude='+longitude+'&latitude='+latitude)">
					<image class="shop-left" :src="cloudStorage+item.businessFace"></image>
					<view class="shop-right">
						<view class="shop-title">{{item.businessName}}</view>

						<view class="shop-address">
							<view class="address-left">
								<image
									src="https://job-1306862033.cos.ap-chengdu.myqcloud.com/miniprogram-static/alliance/site.png">
								</image>
								<view class="address">{{item.address}}</view>
							</view>
							<view class="address-right">
								<image
									src="https://job-1306862033.cos.ap-chengdu.myqcloud.com/miniprogram-static/alliance/distance.png">
								</image>
								<view class="distance">{{item.distance}}km</view>
							</view>
						</view>
					</view>
				</view>

			</view>
		</template>
		<view class="small-title">消费须知</view>
		<view class="info-block">
			<view class="special-text">
				<mp-html :content="info.consumeNoticeDetails"></mp-html>
			</view>
		</view>
		<view class="small-title" style="height: 80rpx;"></view>
		<view class="step fixed-button" @click="buyNowActivity">立即抢购</view>
		<view class="cancel-pop" v-if="isShow">
			<view class="cancel-block">
				<view class="cancel-title">
					<view class="title">请输入支付密码</view>
					<view @click="closeShow">
						<image
							src="https://job-1306862033.cos.ap-chengdu.myqcloud.com/miniprogram-static/alliance/close.png"
							mode=""></image>
					</view>
				</view>
				<view class="pop-info">
					<view class="input">
						<view class="input-item" v-for="(item,index) in 6" :key="index" :data-index="index">
							<view class="input-value">{{password[index]}}</view>
						</view>
						<input class="input-password" maxlength="6" v-model="paymentPassword" type="number"
							@input="setValue" focus="true" />
					</view>
				</view>
				<view class="btn" @click="buyNowActivity">确定</view>
			</view>
		</view>
	</view>
</template>

<script>
	import {
		buyNowActivity,
		getActivityById
	} from '@/api/acticity.js';
	import {
		reservationActivity
	} from '@/api/activities.js';
	import {
		getStorage
	} from '@/util/auth.js'
	import {
		getUserInfo
	} from '@/api/home.js';
	export default {
		data() {
			return {
				id: 0,
				info: {
					activityName: '',
					price: '',
					originalPrice: '',
					serviceGuarantee: '',
					consumeNotice: '',
					validPeriod: '',
					groupBuyingDetails: '',
					consumeNoticeDetails: '',
					groupBuyingCategoryList: [],
					businessList: []
				},
				latitude: 39.74453,
				longitude: 98.51061,
				count: 1,
				password: [],
				isShow: false,
				paymentPassword: '',
				isPassword: null,
				type: 0,
				expirationTime: '', // 设置你的过期时间
				hours: 0,
				minutes: 0,
				seconds: 0,
			}
		},
		methods: {
			countdown() {
				const now = new Date();
				const expiration = new Date(this.expirationTime);
				const timeDiff = expiration - now;

				if (timeDiff <= 0) {
					// 倒计时结束
					this.hours = 0;
					this.minutes = 0;
					this.seconds = 0;
					clearInterval(this.intervalId);
					return;
				}

				this.hours = Math.floor((timeDiff % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
				this.minutes = Math.floor((timeDiff % (1000 * 60 * 60)) / (1000 * 60));
				this.seconds = Math.floor((timeDiff % (1000 * 60)) / 1000);
			},
			confirm() {
				if (getStorage('token')) {
					if (this.isPassword == 1) {
						this.isShow = true;
					} else {
						uni.showModal({
							title: '提示',
							content: '请先设置支付密码！',
							cancelText: '先逛逛',
							confirmText: '去设置',
							success(res) {
								if (res.confirm) {
									uni.navigateTo({
										url: '/page_other/password/password'
									})
								}
							}
						})
					}
				} else {
					uni.showModal({
						title: '提示',
						content: '需要先登录！',
						cancelText: '先逛逛',
						confirmText: '去登录',
						success(res) {
							if (res.confirm) {
								uni.reLaunch({
									url: '/page_other/mine/mine'
								})
							} else {
								uni.navigateBack();
							}
						}
					})
				}
			},
			closeShow() {
				this.isShow = false
				this.password = ''
				this.paymentPassword = ''
			},
			setValue(e) {
				var value = e.detail.value
				this.value = value
				this.focusIndex = value.length
				this.focus = value.length < 6
				this.password = '*'.repeat(value.length)
				this.paymentPassword = e.detail.value
			},
			//商家详情
			getActivityById() {
				uni.showLoading({
					title: '加载中',
					mask: true
				})
				getActivityById({
					id: this.id,
					longitude: this.longitude,
					latitude: this.latitude,
				}).then((res) => {
					this.info = res
					this.expirationTime = this.info.endTime
				})
			},
			getUser() {
				getUserInfo().then((res) => {
					this.isPassword = res.isPassword
				})
			},
			//立即抢购活动
			buyNowActivity() {
				var that = this;

				if (this.isVip == 1) {
					uni.showModal({
						title: '提示',
						content: '您确定购买此套餐吗？',
						cancelText: '取消',
						confirmText: '确定',
						success(res) {
							if (res.confirm) {
								uni.showLoading({
									title: '抢购中',
									mask: true
								})
								buyNowActivity({
									id: that.id,
									count: that.count,
									password: that.paymentPassword
								}).then((res) => {
									this.isShow = false
									that.showToast('抢购成功', () => {
										uni.redirectTo({
											url: '/page_other/merchant_activity/index'
										})
									}, 1000)
								})
							}
						}
					})
				} else {
					uni.showModal({
						title: '提示',
						content: '需要先开通会员！',
						cancelText: '先逛逛',
						confirmText: '去开通',
						success(res) {
							if (res.confirm) {
								uni.navigateTo({
									url: '/page_other/become_member/index'
								})
							}
						}
					})
				}
			},
			//立即预约
			appointment() {
				var that = this;
				uni.showModal({
					title: '提示',
					content: '您确定预约此套餐吗？',
					cancelText: '取消',
					confirmText: '确定',
					success(res) {
						if (res.confirm) {
							uni.showLoading({
								title: '预约中',
								mask: true
							})
							reservationActivity({
								businessId: that.info.businessId,
								activityId: that.info.id,
							}).then((res) => {
								this.isShow = false
								that.showToast('预约成功', () => {
									uni.reLaunch({
										url: '/page_other/merchant_activity/index'
									})
								}, 1000)
							})
						}
					}
				})
			},
		},

		onLoad(e) {
			this.id = e.id
			if (e.type) {
				this.type = e.type
			}
			if (e.latitude) {
				this.latitude = e.latitude
				this.longitude = e.longitude
			} else {
				this.latitude = getStorage('latitude')
				this.longitude = getStorage('longitude')
			}
			this.getActivityById()
			const userInfo = getStorage('user');
			this.isVip = userInfo.vip;

			this.countdown();
			this.intervalId = setInterval(this.countdown, 1000);
		},
		onUnload() {
			clearInterval(this.intervalId); // 组件销毁时清除定时器
		},
		onShow() {
			if (getStorage('token')) {
				this.getUser()
			}
		}
	}
</script>

<style lang="scss" scoped>
	.content {
		width: 100%;
		padding: 30rpx;
		position: relative;

		.fixed-button {
			position: fixed;
			bottom: 20rpx;
			left: 50%;
			transform: translateX(-50%);
			width: 450rpx;
			/* 按钮的宽度 */
			height: 100rpx;
			/* 按钮的高度 */
			display: flex;
			background: linear-gradient(90deg, #FF2F34 0%, #FF6B18 100%);
			box-shadow: 0px 4px 10px 0px rgba(0, 0, 0, 0.1);
			color: #fff;
			border-radius: 60rpx;
			justify-content: center;
			align-items: center;
			font-size: 32rpx;
			margin: 0 auto;
			/* 如果需要调整按钮的位置，可以调整这里的值 */

		}
	}

	.step {
		width: 450rpx;
		height: 100rpx;
		display: flex;
		background: linear-gradient(90deg, #FF2F34 0%, #FF6B18 100%);
		box-shadow: 0px 4px 10px 0px rgba(0, 0, 0, 0.1);
		color: #fff;
		margin: 30rpx auto;
		border-radius: 60rpx;
		justify-content: center;
		align-items: center;
		font-size: 32rpx;

	}

	.menu-block {
		width: 100%;
		margin-bottom: 20rpx;
		background: #fff;
		border-radius: 20rpx;
		padding: 30rpx;

		.title {
			color: #1A1A1A;
			font-size: 36rpx;
			margin-bottom: 30rpx;
			font-weight: bold;
		}

		.active-bg {
			width: 100%;
			border-radius: 20rpx;
		}

		.price-block {
			position: relative;
			width: 100%;
			margin: -60rpx 0 30rpx 0;
			padding: 20rpx 40rpx;
			border-radius: 20rpx;
			background: linear-gradient(90deg, #FF2F34 0%, #FF6B18 50%);
			z-index: 9;
			display: flex;
			justify-content: space-between;

			.price-title {
				width: 60%;

				.price-top {
					display: flex;
					align-items: center;
					margin-top: 15rpx;

					.current-price {
						font-size: 36rpx;
						color: #FFFFFF;
						margin-right: 8rpx;
					}

					.original-price {

						font-size: 24rpx;
						color: #FFFFFF;

						.price-line {

							top: 22rpx;
							background-color: #FF2F34;
							width: 55rpx;
							height: 3rpx;
						}
					}
				}

				.price-bottom {

					font-size: 26rpx;
					color: #FFFFFF;
				}

			}

			.price-time {

				height: 120rpx;
				width: 40%;
				display: flex;
				flex-direction: column;

				.price-time-desc {
					width: 100%;

					font-size: 32rpx;
					color: #FFFFFF;
					text-align: center;
					margin-bottom: 10rpx;
				}

				.price-time-countdown {
					width: 100%;
					height: 100%;
					display: flex;
					justify-content: center;

					.time-point {
						font-size: 42rpx;
						color: #FFFFFF;
						margin-right: 10rpx;
					}

					.time-line {
						height: 60rpx;
						width: 80rpx;
						border-radius: 15rpx;
						background-color: #FF2F34;
						margin-right: 10rpx;
						display: flex;
						justify-content: center;

						.text {
							text-align: center;
							line-height: 60rpx;
							font-weight: bold;
							font-size: 42rpx;
							color: #FFFFFF;
						}
					}
				}
			}
		}

		.tips-block {
			display: flex;
			margin-bottom: 20rpx;

			.tips {
				width: 140rpx;
				font-size: 28rpx;
				color: #1A1A1A;
			}

			.hint {
				width: calc(100% - 140rpx);
				font-size: 28rpx;
				color: #767676;
			}
		}

		.line {
			width: 100%;
			height: 2rpx;
			background-color: #EEEEEE;
			margin-bottom: 30rpx;
		}

		.tips-title {
			color: #1A1A1A;
			font-size: 28rpx;
			margin-bottom: 24rpx;

		}

		.tips {
			font-size: 30rpx;
			color: #767676;
			margin-bottom: 10rpx;
		}


	}

	.small-title {
		color: #1A1A1A;
		font-size: 32rpx;
		margin-bottom: 20rpx;
		font-weight: bold;
		margin-top: 20rpx;
	}

	.info-block {
		background-color: #FFFFFF;
		border-radius: 20rpx;
		margin-bottom: 20rpx;
		width: 100%;
		padding: 30rpx;

		.special-title {
			display: flex;
			align-items: center;
			margin-bottom: 26rpx;

			.dian {
				border-radius: 100rpx;
				background-color: #1A1A1A;
				width: 10rpx;
				height: 10rpx;
				margin: 0 25rpx;
			}

			.title {
				color: #1A1A1A;
				font-size: 28rpx;
				font-weight: bold;
			}
		}

		.special-text {
			color: #1A1A1A;
			font-size: 28rpx;
			font-weight: normal;
			text-indent: 2em;
		}

		.line {
			width: 100%;
			background-color: #EEEEEE;
			height: 2rpx;
			margin: 30rpx 0;
		}

		.shop {
			display: flex;
			margin-bottom: 30rpx;

			.shop-left {
				width: 120rpx;
				height: 120rpx;
				border-radius: 20rpx;
				margin-right: 10rpx;
			}

			.shop-right {
				width: calc(100% - 90rpx);

				.shop-title {
					font-size: 28rpx;
					color: #1A1A1A;
					font-weight: bold;
				}

				// .shop-label{
				// 	font-size: 24rpx;
				// 	color: #767676;
				// 	margin-left: 20rpx;
				// }
				.shop-address {
					width: 100%;
					display: flex;
					justify-content: space-between;
					margin-top: 30rpx;

					.address-left {
						display: flex;
						justify-content: center;
						align-items: center;

						image {
							width: 40rpx;
							height: 40rpx;
							margin-right: 8rpx;
						}

						.address {
							font-size: 24rpx;
							width: 350rpx;
							color: #767676;
							overflow: hidden;
							/* 隐藏超出部分 */
							white-space: nowrap;
							/* 不换行 */
							text-overflow: ellipsis;
							/* 文字超出部分显示省略号 */
						}
					}

					.address-right {
						display: flex;
						justify-content: center;
						align-items: center;

						image {
							width: 20rpx;
							height: 20rpx;
							margin-right: 4rpx;
						}

						.distance {
							font-size: 24rpx;
							color: #767676;
						}
					}
				}
			}
		}
	}

	.group-block {
		background-color: #FFFFFF;
		padding: 20rpx;
		border-radius: 30rpx;

		.group-list:last-child {
			border-bottom: none;
		}

		.group-list {
			border-bottom: 1px solid #F5F5F5;
			padding-bottom: 20rpx;

			.group-title-item {
				display: flex;
				align-items: center;
				margin-bottom: 20rpx;
				margin-top: 20rpx;

				.round {
					width: 6rpx;
					height: 6rpx;
					border-radius: 50%;
					background-color: #767676;
					margin-right: 14rpx;
				}

				.group-title {
					font-size: 30rpx;
					font-weight: bold;
				}
			}


			.group-item {
				display: flex;
				align-items: center;
				justify-content: space-between;
				font-size: 28rpx;
				margin-bottom: 14rpx;
				margin-left: 20rpx;

				.count-item {
					display: flex;
					align-items: center;

					.count {
						font-size: 25rpx;
						color: #767676;
						margin-right: 20rpx;
					}

					.price1 {
						display: flex;
						justify-content: flex-end;
						width: 120rpx;
					}
				}
			}
		}
	}

	.cancel-pop {
		position: fixed;
		left: 0;
		top: 0;
		background: rgba(0, 0, 0, 0.2);
		z-index: 999999;
		width: 100vw;
		height: 100vh;
		padding-left: 30rpx;
		padding-right: 30rpx;

		.cancel-block {
			width: 90%;
			background: #fff;
			padding: 30rpx 40rpx;
			z-index: 99;
			height: 300rpx;
			position: fixed;
			bottom: 45%;
			box-shadow: 0 0 6px 0px #dadada;
			border-radius: 30rpx;
			display: flex;
			flex-direction: column;

			.cancel-title {
				display: flex;
				justify-content: space-between;

				.title {
					font-size: 34rpx;
					font-weight: bold;
				}

				image {
					width: 40rpx;
					height: 40rpx;
				}
			}

			.pop-info {
				margin-top: 10rpx;
				display: flex;
				height: 100rpx;
				justify-content: center;
				margin-bottom: 10rpx;

				/* 密码输入框 */
				.input {
					margin-top: 10rpx;
					/* padding: 0 60rpx; */
					position: relative;
				}

				.input-item {
					position: relative;
					display: inline-block;
					width: 40px;
					height: 40px;
					/* border-bottom: 2rpx solid #333;*/
					/* 下划线效果*/
					background: rgba(0, 0, 0, 0.04);
					/* 灰色框效果 */
					border-radius: 4px;
				}

				.input-item:not(:first-child) {
					margin-left: 8px;
				}

				.input-value {
					display: inline-block;
					position: absolute;
					left: 50%;
					top: 50%;
					transform: translate(-50%, -50%);
					font-size: 28rpx;
				}

				.input-password {
					position: absolute;
					left: -360rpx;
					top: 0;
					height: 90rpx;
					width: 880rpx;
					/* opacity: 0; */
					/* 真机opacity 无效 */

					/* 解决真机opacity 无效的问题 */
					background-color: rgba(255, 255, 255, 0.1);
					color: rgba(255, 255, 255, 0.1);
				}

				.focus {
					width: 2rpx;
					height: 50rpx;
					background-color: #333;
					position: absolute;
					left: 50%;
					top: 50%;
					transform: translate(-50%, -50%);
				}

				.show {
					display: block;
					animation: blink 1s linear infinite;
				}

				.hide {
					display: none;
				}

				@keyframes blink {

					0%,
					50% {
						opacity: 1;
					}

					50.01%,
					to {
						opacity: 0;
					}
				}

			}

			.btn {
				background-color: #FF2F34;
				width: 60%;
				height: 80rpx;
				border-radius: 50rpx;
				color: #fff;
				display: flex;
				margin: 0 auto;
				border-radius: 60rpx;
				justify-content: center;
				align-items: center;
			}
		}
	}
</style>